module FileChooser 
    (
     openFileDialog
    , saveFileDialog
    ) where

import Graphics.UI.Gtk

saveFileDialog:: String -> (Maybe String) -> (FilePath -> IO ()) -> IO ()
saveFileDialog title fp action = fileDialog title "gtk-save" FileChooserActionSave action fp

openFileDialog:: String -> (Maybe String) -> (FilePath -> IO ()) -> IO ()
openFileDialog title fp action = fileDialog title "gtk-open" FileChooserActionOpen action fp


fileDialog thetitle accepttitle accepttype action filterPattern = do
  dialog <- fileChooserDialogNew
              (Just $ thetitle)
              Nothing
              accepttype
	      [("gtk-cancel"       
	       ,ResponseCancel)
	      ,(accepttitle                                  
	       , ResponseAccept)]
  case filterPattern of 
    Nothing -> return ()
    Just filterPattern' ->
        do
          filt <- fileFilterNew
          fileFilterAddPattern filt filterPattern'
          set dialog [fileChooserFilter := filt]
  fileChooserSetPreviewWidgetActive dialog True
  widgetShow dialog
  response <- dialogRun dialog
  case response of 
    ResponseAccept -> do 
                  Just fileName <- fileChooserGetFilename dialog
                  action fileName
    ResponseCancel -> return ()
    ResponseDeleteEvent -> return ()
  widgetHide dialog

